﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RunCircle
{
    public class AI_Player
    {
        protected Player body;
        protected Player enemy;
        protected Track  road;

        protected Random rnd;
        public bool Enabled { set; get; }
        protected float timeCounter;

        protected int m_AILevel;
        protected int m_AIMaxLevel;

        public AI_Player(Player player,Player enemyPlayer,Track track)
        {
            body = player;
            enemy = enemyPlayer;
            road = track;
            rnd = new Random();
            Enabled = true;
            timeCounter = 0;

            m_AIMaxLevel = 3;
            m_AILevel = rnd.Next(m_AIMaxLevel);
            if (m_AILevel == 3)
            {
                int i = 100;
            }
        }
        //
        public void update(float dt)
        {
            if (Enabled == false) return;
            //
            timeCounter+=dt;

            if (body.m_FSM.GetCurPlayerStatus() != Player_Run.Instance())
            {
                return;
            }
            //
            float AtotalAngle = body.getTotalRunAngle();
            float BtotalAngle = enemy.getTotalRunAngle();
            float offsetAngle = AtotalAngle - BtotalAngle;
            //int value = AnalyseValue(timeCounter, body.GetPower(), offsetAngle);
            //首先判断前方道路
            int dist = rnd.Next(20);
            float angle = body.rotation - dist;//前面的距离
            if (angle > 360.0f)
            {
                angle = angle - 360.0f;
            }
            else if (angle < -360.0f)
            {
                angle = angle + 360.0f;
            }
            Trap TT = road.getTrap(angle);
            if (TT != null)
            {
                switch (TT.getTrapType())
                {
                    case "Trap_Banana":
                        {
                            if (m_AILevel == 0)
                            {
                                if (rnd.Next(100) > 30)
                                {
                                    body.useJump();
                                }
                            }
                            else if (m_AILevel == 1)
                            {
                                if (rnd.Next(100) > 10)
                                {
                                    body.useJump();
                                }
                            }
                            else if (m_AILevel == 2)
                            {
                                if (body.GetPower() == 4)
                                {
                                    if (body.useSkill() == 4)
                                    {
                                        body.m_FSM.SetPlayerStatus(Player_SpeedUp.Instance());
                                    }  
                                }
                                else if (rnd.Next(100) > 10)
                                {
                                    body.useJump();
                                }
                            }
                            
                        }
                        break;
                    case "Trap_Board":
                        break;
                }
            }
            else
            {   //没事儿 也瞎胡跳
                if (rnd.Next(1000) < 13)
                {
                    body.useJump();
                }
            }
            //
            int nSkill = 0;
            
            switch(body.GetPower())
            {
                case 0:
                    break;
                case 1:
                    if (m_AILevel == 0)
                    {
                        if (rnd.Next(1000) < 25)
                        {
                            nSkill = body.useSkill();
                        }
                    }
                    else
                    {
                        if (rnd.Next(1000) < 5)
                        {
                            nSkill = body.useSkill();
                        }
                    }
                    
                    break;
                case 2:
                    if (m_AILevel == 0)
                    {
                        if (rnd.Next(1000) < 70)
                        {
                            nSkill = body.useSkill();
                        }
                    }
                    else if (m_AILevel == 1)
                    {
                        if (rnd.Next(1000) < 40)
                        {
                            nSkill = body.useSkill();
                        }
                    }
                    else
                    {
                        if (rnd.Next(1000) < 5)
                        {
                            nSkill = body.useSkill();
                        }
                    }
                    break;
                case 3:
                    if (m_AILevel == 0)
                    {
                        if (body.m_FSM.GetCurPlayerStatus() == Player_Fall.Instance() ||
                            body.m_FSM.GetCurPlayerStatus() == Player_Electric.Instance())
                        {
                            if (rnd.Next(1000) < 100)
                            {
                                if (body.useSkill() == 3)
                                {
                                    body.m_FSM.SetPlayerStatus(Player_Anger.Instance());
                                }
                            }
                        }
                    }
                    else if (m_AILevel == 1)
                    {
                        if (body.m_FSM.GetCurPlayerStatus() == Player_Fall.Instance() ||
                            body.m_FSM.GetCurPlayerStatus() == Player_Electric.Instance())
                        {
                            if (rnd.Next(1000) < 300)
                            {
                                if (body.useSkill() == 3)
                                {
                                    body.m_FSM.SetPlayerStatus(Player_Anger.Instance());
                                }
                            }
                        }
                    }
                    else
                    {
                        if (body.m_FSM.GetCurPlayerStatus() == Player_Fall.Instance() ||
                            body.m_FSM.GetCurPlayerStatus() == Player_Electric.Instance())
                        {
                            if (rnd.Next(1000) < 500)
                            {
                                if (body.useSkill() == 3)
                                {
                                    body.m_FSM.SetPlayerStatus(Player_Anger.Instance());
                                }
                            }
                        }
                    }
                    break;
                case 4:
                    {
                        if (m_AILevel == 0)
                        {
                            if (rnd.Next(1000) < 200)
                            {
                                if (body.useSkill() == 4)
                                {
                                    body.m_FSM.SetPlayerStatus(Player_SpeedUp.Instance());
                                }   
                            }
                        }
                        else
                        {
                            if (body.useSkill() == 4)
                            {
                                body.m_FSM.SetPlayerStatus(Player_SpeedUp.Instance());
                            }   
                        }
                    }
                    break;
            }
            //
            if (nSkill != 0)
            {
                road.CreateTrap(body.rotation, (uint)(nSkill-1));
            }

            //
            


            
        }

        public void restart()
        {
            m_AILevel = rnd.Next(m_AIMaxLevel);
        }

        public void clear()
        {
            body = null;
            enemy = null;
            road = null;
        }
        /*
        //  还没有写好·····只是一个想法···哇咔咔
        //
        //如果返回值是 0- 40 则是 落后的状态 //冲刺
        //如果返回值是 40-65 则是 平常的状态 //乱扔香蕉皮，乱扔指示牌
        //如果返回值是 65-100 则是 激进的状态 //快速起身，乱扔香蕉皮
        public int AnalyseValue(float timeCounter,    //代理了多久，进行了多长时间了
                                int playerPower,    //玩家现有的点数 0-4
                                float AngleLeft) //剩余的角度 
                                                 //正数是领先
                                                 //负数是落后
        {
            int value = 0;
            float tmpCounter = timeCounter;
            const float C1 = 1.007f;
            const float C2 = 10.0f;
            const float C3 = 0.3f;

            //以指数方式增长，越到后期，越想胜利
            double C11 = Math.Pow(tmpCounter, C1);
            double C22 = playerPower * C2;
            double C33 = (AngleLeft+180.0f) * C3;

            value = Math.Min((int)(C11 + C22 + C33),100);

            return value;
              
        }
         */

    }
}
